#include "ds18b20.h"
#include "one_wire.h"

enum Command {
  SkipROM = 0xCC,
  Convert = 0x44,
  ReadScratchPad = 0xBE,
};

void ds18b20_convert(void) {
  one_wire_init();
  one_wire_write_byte(SkipROM);
  one_wire_write_byte(Convert);
}

f32 ds18b20_read(void) {
  u8 TLSB, TMSB;
  one_wire_init();
  one_wire_write_byte(SkipROM);
  one_wire_write_byte(ReadScratchPad);
  TLSB = one_wire_read_byte();
  TMSB = one_wire_read_byte();
  u16 temp = (TMSB << 8) | TLSB;
  return (f32)(temp / 16.0);
}
